/*
 * STrs.c
 *
 * Created: 2/28/2013 11:11:51 PM
 *  Author: RISNIF
 */ 


#include <avr/io.h>
#include "set_clock.h"
#include "set_adc.h"
#include "set_uart.h"
#include "set_timer.h"

void EnableAllInterrupt(void);
void DisableAllInterrupt(void);

int main(void)
{
	Init_Clock_32MHz();
	myEventInit_ADC();
	myInit_UART();
	
    while(1)
    {
        //TODO:: Please write your application code 
    }
}

ISR(USARTC0_RXC_vect)
{
	static char data=0;
	
	data = USARTC0.DATA;	//Baca Data dari USART
	rx_buffer0[rx_wr_index0++]=data;	//Simpan Data ke Buffer
	if (rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
	if (++rx_counter0 == RX_BUFFER_SIZE0)	//Increment Counter
	{
		rx_counter0=0;
		rx_buffer_overflow0=1;
	}
}

ISR(ADCA_CH0_vect)
{
	static int adc_val=0;
		
	adc_val = ADCA.CH0RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc_val;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	while(!ADCA.CH1.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH1.INTFLAGS = 1; //Clear Flag
	adc_val = ADCA.CH1RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc_val;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	while(!ADCA.CH2.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH2.INTFLAGS = 1; //Clear Flag
	adc_val = ADCA.CH2RES; //Membaca Hasil
	
	adc_buffer0[adc_wr_index0++]=adc_val;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
	
	while(!ADCA.CH3.INTFLAGS); //Menunggu Proses konversi selesai
	ADCA.CH3.INTFLAGS = 1; //Clear Flag
	adc_val = ADCA.CH3RES; //Membaca Hasil	
	
	adc_buffer0[adc_wr_index0++]=adc_val;	//Simpan Data ke Buffer
	if (adc_wr_index0 == ADCA_BUFFER_SIZE0) adc_wr_index0=0;
	if (++adc_counter0 == ADCA_BUFFER_SIZE0)	//Increment Counter
	{
		adc_counter0=0;
		adc_buffer_overflow0=1;
	}
}



void EnableAllInterrupt(void)
{
	PMIC.CTRL = PMIC_LOLVLEN_bm;
	sei();
}

void DisableAllInterrupt(void)
{
	cli();
}